package com.gitee.wsl.doc.prosemirror.state.command

import com.gitee.wsl.doc.prosemirror.model.content.ext.liftTarget
import com.gitee.wsl.doc.prosemirror.state.Command
import com.gitee.wsl.doc.prosemirror.state.CommandDispatch
import com.gitee.wsl.doc.prosemirror.state.EditorState
import com.gitee.wsl.doc.prosemirror.transform.ext.lift

/**
 * 将选中的块或选择的最接近的可提升祖先块从其父节点中提升出来
 * @param state 编辑器状态
 * @param dispatch 事务处理回调函数
 * @return 操作是否成功执行
 */
object Lift: Command{
    override fun exec(
        state: EditorState,
        dispatch: CommandDispatch?,
    ): Boolean {
        val from = state.selection._from
        val to = state.selection._to
        val range = from.blockRange(to)
        val target = range?.liftTarget()

        if (target == null) return false

        if (dispatch != null) {
            dispatch(state.tr.lift(range, target).scrollIntoView())
        }
        return true
    }

}

val Command.Companion.lift get() = Lift